/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bef.dtgenerator.core.rtgen.entitygen.dataserializer.serializer;

import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.spi.jsonser.entity.AbstractEntitySerializerItem;
import java.util.ArrayList;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.ParameterizedType;
import org.eclipse.jdt.core.dom.ReturnStatement;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;

public class ConvertGetTypesGenerator extends ClassMethodGenerator {

  private java.util.ArrayList<java.lang.Class> serTypes;

  public ConvertGetTypesGenerator(java.util.ArrayList<java.lang.Class> serTypes) {
    this.serTypes = serTypes;
  }

  @Override
  protected Block buildMethodBody() {
    Block block = ast.newBlock();
    addNewListStatement(block);
    addAddListItemsStatements(block);
    addReturnListStatement(block);
    return block;
  }

  private void addNewListStatement(Block block) {
    VariableDeclarationFragment variableDeclarationFragment = ast.newVariableDeclarationFragment();
    variableDeclarationFragment.setName(ast.newSimpleName("list"));

    ClassInstanceCreation classInstanceCreation = ast.newClassInstanceCreation();
    ParameterizedType parameterizedType1 = ast.newParameterizedType(
        new com.inspur.edp.caf.generator.baseInfo.TypeInfo(ArrayList.class).getType(ast));
    parameterizedType1.typeArguments().add(
        new com.inspur.edp.caf.generator.baseInfo.TypeInfo(AbstractEntitySerializerItem.class)
            .getType(ast));
    classInstanceCreation.setType(parameterizedType1);
    variableDeclarationFragment.setInitializer(classInstanceCreation);

    VariableDeclarationStatement variableDeclarationStatement = ast
        .newVariableDeclarationStatement(variableDeclarationFragment);
    ParameterizedType parameterizedType = ast.newParameterizedType(
        new com.inspur.edp.caf.generator.baseInfo.TypeInfo(ArrayList.class).getType(ast));
    parameterizedType.typeArguments().add(
        new com.inspur.edp.caf.generator.baseInfo.TypeInfo(AbstractEntitySerializerItem.class)
            .getType(ast));
    variableDeclarationStatement.setType(parameterizedType);
    block.statements().add(variableDeclarationStatement);
  }

  private void addAddListItemsStatements(Block block) {
    for (Class item : serTypes) {
      MethodInvocation methodInvocation = ast.newMethodInvocation();
      methodInvocation.setExpression(ast.newSimpleName("list"));
      methodInvocation.setName(ast.newSimpleName("add"));
      ClassInstanceCreation classInstanceCreation = ast.newClassInstanceCreation();
      classInstanceCreation
          .setType(ast.newSimpleType(ast.newName(item.getTypeName())));
      methodInvocation.arguments().add(classInstanceCreation);
      block.statements().add(ast.newExpressionStatement(methodInvocation));
    }
  }

  private void addReturnListStatement(Block block) {
    ReturnStatement returnStatement = ast.newReturnStatement();
    returnStatement.setExpression(ast.newSimpleName("list"));
    block.statements().add(returnStatement);
  }

  @Override
  protected java.util.ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
    return new java.util.ArrayList<Modifier.ModifierKeyword>(java.util.Arrays.asList(
        new Modifier.ModifierKeyword[]{Modifier.ModifierKeyword.PRIVATE_KEYWORD,
            Modifier.ModifierKeyword.STATIC_KEYWORD}));
  }

  @Override
  protected String getMethodName() {
    return "getTypes";
  }

  @Override
  protected ArrayList<ParameterInfo> getParameterCollection() {
    return null;
  }

  @Override
  protected TypeInfo getReturnType() {
    TypeInfo typeInfo = new TypeInfo(ArrayList.class.getTypeName());
    typeInfo.setIsParameterizedType(true);
    typeInfo.getArguments().add(new TypeInfo(AbstractEntitySerializerItem.class));
    return typeInfo;
  }
}
